﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Support;
using Microsoft.SharePoint.Taxonomy;
using MS.InformationManagement.DAL.DocumentCenter;

namespace MS.InformationManagement.Core.Actions
{
    public class VoBoAction : IBaseCommand
    {

        #region Fields

        Stack<ICommand> _transactionCommands;
        Guid _sourceLibraryId;
        int[] _selectedIds;
        string _notes;
        string _targetUser;

        #endregion

        #region Constructors

        public VoBoAction(Guid sourceLibraryId, int[] SelectedIds, string notes, string targetUser)
        {
            _sourceLibraryId = sourceLibraryId;
            _selectedIds = SelectedIds;
            _notes = notes;
            _targetUser = targetUser;
        }

        #endregion

        #region Implemented Methods

        public void Execute()
        {
            try
            {

                _transactionCommands = new Stack<ICommand>();
                //SPUserToken currentUserToken = SPContext.Current.Web.CurrentUser.UserToken;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
                    {
                        using (SPWeb currentWeb = currentSite.OpenWeb())
                        {

                            currentSite.AllowUnsafeUpdates = true;
                            currentWeb.AllowUnsafeUpdates = true;

                            for (int i = 0; i < _selectedIds.Length - 1; i++)
                            {
                                //Get source and target data
                                SPList targetLibrary = UserLibraryHelper.GetUserList(_targetUser, currentWeb);
                                SPFile sourceFile = currentWeb.Lists[_sourceLibraryId].GetItemById(_selectedIds[i]).File;

                                //Update source document
                                VoBoEditSourceDocumentCommand editionCommand = new VoBoEditSourceDocumentCommand(_selectedIds[i], _sourceLibraryId, _notes, _targetUser, currentWeb);
                                editionCommand.Execute();
                                _transactionCommands.Push(editionCommand);

                                //Creating request for assign document
                                VoBoAssignDocumentCommand assignDocumentCommand = new VoBoAssignDocumentCommand(sourceFile, targetLibrary);
                                assignDocumentCommand.Execute();
                                _transactionCommands.Push(assignDocumentCommand);

                                //Assign permissions for assigned user
                                VoBoAssignDocumentsPermissionCommand assingPermissionsCommand = new VoBoAssignDocumentsPermissionCommand(_targetUser, sourceFile.Item);
                                assingPermissionsCommand.Execute();
                                _transactionCommands.Push(assingPermissionsCommand);

                                currentSite.AllowUnsafeUpdates = false;
                                currentWeb.AllowUnsafeUpdates = false;
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                Undo();
                throw ex;
            }
        }

        public void Undo()
        {
            foreach (var command in _transactionCommands)
                command.Undo();
        }

        #endregion
 
    }
}
